package com.papper.mng.controller.base;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.papper.common.bean.base.BaseConstMsg;
import com.papper.common.constant.SystemConstants;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.DwzPage;
import com.papper.common.service.base.BaseConstMsgService;
import com.papper.common.utils.tool.StringTool;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * @Author:LaJiao
 * @Date:2022-03-29 10:06:49
 * @Description:系统消息CODE定义
 */
@Controller
@RequestMapping("/base/const/msg")
public class BaseConstMsgController extends BaseAction {

    @Autowired
    BaseConstMsgService baseConstMsgService;

    /**
     * 系统消息CODE定义
     *
     * @return
     */
    @RequestMapping(value = "")
    public String execute(HttpServletRequest _request, Model model) {
        // 字段查询条件
        Map<String, Object> objects = Maps.newHashMap();
        extraParams(objects, _request, BaseConstMsg.class);
        modelParm(objects, model);
        // 分页查询条件
        int pageNum    = StringTool.parseInteger(_request.getParameter("pageNum"), 1);
        int numPerPage = StringTool.parseInteger(_request.getParameter("numPerPage"), SystemConstants.PAGE_LIMIT_16);
        // 数据查询组装
        Example                example = buildExample(objects, model, BaseConstMsg.class);
        PageInfo<BaseConstMsg> pageRtn = baseConstMsgService.findPage(pageNum, numPerPage, example);
        model.addAttribute("page", new DwzPage(pageNum, numPerPage, pageRtn.getTotal()));
        model.addAttribute("records", pageRtn.getList());
        return render("base/const/msg/index");
    }

    /**
     * 系统消息CODE定义_新增
     */
    @GetMapping(value = "/add")
    public String add_get() {
        return render("base/const/msg/add");
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public DwzResponse add_post(HttpServletRequest _request, BaseConstMsg baseConstMsg) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }

        // 添加系统消息CODE定义
        try {
            baseConstMsg.setCreateTime(new Date());
            baseConstMsg.setModifyTime(new Date());
            baseConstMsgService.save(baseConstMsg);
            return new DwzResponse("200", "系统消息CODE定义添加成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "系统消息CODE定义添加失败:" + e.getMessage() + "!!!");
        }
    }

    /**
     * 系统消息CODE定义_更新
     */
    @GetMapping(value = "/edit")
    public String edti_get(Model model, Long id) {
        BaseConstMsg entity = baseConstMsgService.findById(id);
        model.addAttribute("entity", entity);
        return render("base/const/msg/edit");
    }

    @PostMapping(value = "/edit")
    @ResponseBody
    public DwzResponse edit_post(HttpServletRequest _request, BaseConstMsg baseConstMsg) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (baseConstMsg == null || baseConstMsg.getId() == null) {
            return new DwzResponse("300", "参数异常！");
        }
        try {
            baseConstMsg.setModifyTime(new Date());
            baseConstMsgService.update(baseConstMsg);
            return new DwzResponse("200", "系统消息CODE定义修改成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "系统消息CODE定义修改失败:" + e.getMessage());
        }
    }

    /**
     * 系统消息CODE定义_删除
     *
     * @param id
     * @
     */
    @PostMapping(value = "/del")
    @ResponseBody
    public DwzResponse del_post(HttpServletRequest _request, Long id) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (id == null || id.intValue() <= 0) {
            return new DwzResponse("300", "参数ID为空！");
        }

        BaseConstMsg old = baseConstMsgService.findById(id);

        if (old == null) {
            return new DwzResponse("300", "参数[id]信息异常！");
        }
        try {
            baseConstMsgService.deleteById(id);
            return new DwzResponse("200", "系统消息CODE定义删除成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "系统消息CODE定义删除失败:" + e.getMessage());
        }
    }

    /**
     * 系统消息CODE定义_详情
     *
     * @return
     */
    @GetMapping(value = "/detail")
    public String detail(HttpServletResponse _response, Model model, Long id) {
        BaseConstMsg baseConstMsg = baseConstMsgService.findById(id);
        model.addAttribute("entity", baseConstMsg);
        return render("base/const/msg/detail");
    }

}



